﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.Dept;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;

namespace Frame.Web.Admin.Dept
{
    public partial class Edit : PageEdit
    {
        private DeptBO dept = new DeptBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (PKID != null && PKID != "")
                {
                    DispInfo();
                }
                else
                {
                    UpperID.Value = StrUtil.DecryptDES(Request["UpperID"]);
                    lblUpperName.Text = dept.GetName(UpperID.Value);
                    Orders.Text = "0";
                }
            }
            AddAttr();
        }


        private void AddAttr()
        {

        }

        private void DispInfo()
        {
            DeptInfo info = dept.GetInfo(PKID);

            EntityUtil.SetControl(pnlMain, info);
            if (info.UpperID == 0)
            {
                lblUpperName.Text = "顶级部门没有上级部门";
            }
            else
            {
                lblUpperName.Text = dept.GetName(info.UpperID);
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int upperID = int.Parse(UpperID.Value);
            bool blnFlag = dept.Exist(upperID, Name.Text, PKID);
            if (blnFlag)
            {
                JSUtil.Alert("该部门已存在");
            }
            else
            {
                DeptInfo info = dept.GetInfo(PKID);
                if (Orders.Text.Trim() == "")
                {
                    Orders.Text = "0";
                }
                EntityUtil.SetEntity(pnlMain, info);

                if (upperID != Constants.TREE_ROOT_UPPER_ID)//对于顶级部门的处理
                {
                    DeptInfo parentInfo = dept.GetInfo(upperID);
                    info.Depth = parentInfo.Depth + 1;
                    info.Path = parentInfo.Path + parentInfo.ID + ",";
                }
                dept.Save(info);
                JSUtil.RefreshView();
            }
        }
    }
}
